package com.mac.service;

import com.mac.mapper.MemberMapper;
import com.mac.mapper.UserMapper;
import com.mac.pojo.Member;
import com.mac.pojo.PageBean;
import com.mac.pojo.User;
import com.mac.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

@SuppressWarnings("all")
public class MemberService{

    // 定义日志对象
    private static final Logger LOGGER = LoggerFactory.getLogger("MemberService.Class");
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 通过姓名查找成员对象
     */
    public Member selectByMemberName(String memberName) {

        SqlSession session = factory.openSession();

        MemberMapper memberMapper = session.getMapper(MemberMapper.class);

        Member member;
        try {
            member = memberMapper.selectByMemberName(memberName);
            LOGGER.info("selectByMemberName(memberName) -> succeed");
            session.close();
        } catch (Exception e) {
            LOGGER.error("selectByMemberName(memberName) -> failed",e);
            session.close();
            throw new RuntimeException(e);
        }

        return member;
    }

    public Member selectByUserId(int userId) {
        SqlSession session = factory.openSession();

        MemberMapper memberMapper = session.getMapper(MemberMapper.class);

        Member member;
        try {
            member = memberMapper.selectByUserId(userId);

            LOGGER.info("selectByUserId(userId) ->succeed");

            LOGGER.info("selectByUserId(userId) -> succeed");

            session.close();
        } catch (Exception e) {
            LOGGER.error("selectByUserId(userId)-> failed",e);
            session.close();
            throw new RuntimeException(e);
        }

        return member;
    }

    public ArrayList<Member> selectAllMember() {
        SqlSession sqlSession = factory.openSession();

        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);

        try {
            ArrayList<Member> members = memberMapper.selectAllMember();
            sqlSession.commit();
            LOGGER.info("selectAllMember() -> succeed");
            sqlSession.close();
            return members;
        } catch (Exception e) {
            LOGGER.error("selectAllMember() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改成员的信息
     * @param member 成员类
     */
    public boolean changeMemberInfo(Member member) {
        SqlSession sqlSession = factory.openSession();

        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);

        try {
            memberMapper.changeMemberInfo(member);
            sqlSession.commit();
            LOGGER.info("changeMemberInfo(member) -> succeed");
            sqlSession.close();
            return true;
        } catch (Exception e) {
            LOGGER.error("changeMemberInfo(member) -> failed",e);
            sqlSession.close();
            return false;
        }
    }

    public boolean addMember(Member member) {
        SqlSession sqlSession = factory.openSession();

        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);

        try {
            memberMapper.addMember(member);
            sqlSession.commit();
            LOGGER.info("addMember(member) -> succeed");
            sqlSession.close();
            return true;
        } catch (Exception e) {
            LOGGER.error("addMember(member) -> failed",e);
            sqlSession.close();
            return false;
        }
    }

    public ArrayList<Member> selectAll() {
        SqlSession sqlSession = factory.openSession();
        MemberMapper memeberMapper = sqlSession.getMapper(MemberMapper.class);
        ArrayList<Member> members;
        try {
            members = memeberMapper.selectAll();
            LOGGER.info("userMapper.selectAll() -> succeed");
            sqlSession.close();
            return  members ;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectAll() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public boolean deleteByIds(int[] ids) {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        try {
            userMapper.deleteByIds(ids);
            sqlSession.commit();
            sqlSession.close();
            LOGGER.info("userMapper.deleteByIds(ids) -> succeed");
            return true;
        } catch (Exception e) {
            LOGGER.error("userMapper.deleteByIds(ids) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public PageBean<User> selectByPage(int currentPage, int pageSize) {

        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectByPage(begin,size);

            //6. 查询总记录数
            int totalCount = userMapper.selectTotalCount();

            //7. 封装PageBean对象
            PageBean<User> pageBean = new PageBean<>();
            pageBean.setRows(users);
            pageBean.setTotalCount(totalCount);

            LOGGER.info("userMapper.selectByPage(begin,size) -> succeed");
            sqlSession.close();
            return pageBean;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectByPage(begin,size) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public Integer selectTotalCount() {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer total;
        try {
            total = userMapper.selectTotalCount();
            LOGGER.info("userMapper.selectTotalCount() -> succeed");
            sqlSession.close();
            return total;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectTotalCount() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public ArrayList<Member> selectByPageAndCondition(int begin, int size, Member member) {
        return null;
    }

    public Integer selectTotalCountByCondition(Member member) {
        return 0;
    }

    public PageBean<User> selectByPageAndCondition(int currentPage, int pageSize, User user) {

        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectByPageAndCondition(begin,size,user);


            //6. 查询总记录数
            int totalCount = userMapper.selectTotalCountByCondition(user);

            //7. 封装PageBean对象
            PageBean<User> pageBean = new PageBean<>();
            pageBean.setRows(users);
            pageBean.setTotalCount(totalCount);

            LOGGER.info("selectByPageAndCondition(begin,size,user) -> succeed");
            sqlSession.close();
            return pageBean;
        } catch (Exception e) {
            LOGGER.error("selectByPageAndCondition(begin,size,user) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public Integer selectTotalCountByCondition(User user) {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer total;
        try {
            total = userMapper.selectTotalCountByCondition(user);
            LOGGER.info("userMapper.selectTotalCountByCondition(user) -> succeed");
            sqlSession.close();
            return total;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectTotalCountByCondition(user) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }
}
